package com.bamboo.tool.entity;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.bamboo.tool.db.SqliteConfig;
import lombok.Data;
import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;

@Data
public class InitTableInfo {
    private Map<String, List<TableData>> dicts;

    private List<TableData> annotations = new ArrayList<>();

    public static class TableData {
        private String code;
        private String sql;

        private List<TableData> children =new ArrayList<>();

        public TableData(String code, String sql) {
            this.code = code;
            this.sql = sql;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getSql() {
            return sql;
        }

        public void setSql(String sql) {
            this.sql = sql;
        }

        public List<TableData> getChildren() {
            return children;
        }

        public void setChildren(List<TableData> children) {
            this.children = children;
        }
    }

    public static void getAllSql(TableData tableData, List<String> sqls) {
        if(StrUtil.isNotBlank(tableData.getSql())){
            sqls.add(tableData.getSql());
        }
        if (CollUtil.isNotEmpty(tableData.getChildren())) {
            tableData.getChildren().forEach(e -> {
                getAllSql(e, sqls);
            });
        }
    }

    @SneakyThrows
    public static void main(String[] args) {

        Connection conn = SqliteConfig.getConnection();
        Statement state = conn.createStatement();

        ResultSet resultSet = state.executeQuery("select * from bamboo_dict");
        InitTableInfo initTableInfo = new InitTableInfo();
        Map<String, List<TableData>> dicts = new HashMap<>();
        initTableInfo.setDicts(dicts);

        while (resultSet.next()) {
            String key = resultSet.getString("key");
            String value = resultSet.getString("value");
            String description = resultSet.getString("description");
            String otherInfo = resultSet.getString("other_info");
            List<TableData> tableData1 = dicts.get(key);
            if (CollUtil.isEmpty(tableData1)) {
                dicts.put(key, new ArrayList<>());
                tableData1 = dicts.get(key);
            }
            TableData tableData = new TableData(value, "INSERT INTO bamboo_dict (key, value, description, other_info) VALUES ('"+ key + "', '" + value + "','" + (StrUtil.isEmpty(description) ? "" : description) + "','" + (StrUtil.isEmpty(otherInfo) ? "" : otherInfo) + "');");
            tableData1.add(tableData);
        }


        Map<String, Map<String, TableData>> objectObjectHashMap = new HashMap<>();
        ResultSet frameworkResultSet = state.executeQuery("select * from framework");
        while (frameworkResultSet.next()) {
            String name = resultSet.getString("name");
            String id = resultSet.getString("id");
            String describe = resultSet.getString("describe");
            String frameworkId = UUID.randomUUID().toString();
            TableData tableData = new TableData(name, "INSERT INTO framework (id, name, describe) VALUES ('" + frameworkId + "', '" + name + "', '" + describe + "');");
            Map<String, TableData> objectObjectHashMap1 = new HashMap<>();
            objectObjectHashMap1.put(frameworkId, tableData);
            objectObjectHashMap.put(id, objectObjectHashMap1);

        }
        Map<String, Map<String, TableData>> objectObjectHashMap1 = new HashMap<>();
        ResultSet annotationInfoSettingResultSet = state.executeQuery("select * from annotation_info_setting;");
        while (annotationInfoSettingResultSet.next()) {
            String scope = annotationInfoSettingResultSet.getString("scope");
            String annotationInfoSettingIdOld = annotationInfoSettingResultSet.getString("id");
            String classPath = annotationInfoSettingResultSet.getString("class_path");
            String class_short_name = annotationInfoSettingResultSet.getString("class_short_name");
            String framework_id = annotationInfoSettingResultSet.getString("framework_id");
            String soa_type = annotationInfoSettingResultSet.getString("soa_type");
            String effect = annotationInfoSettingResultSet.getString("effect");
            String other_params = annotationInfoSettingResultSet.getString("other_params");
            Map<String, TableData> stringTableDataMap = objectObjectHashMap.get(framework_id);
            stringTableDataMap.forEach((key, value) -> {
                List<TableData> children = value.getChildren();
                long count = children.stream().filter(e -> e.getCode().equals(scope)).count();
                if (count < 1) {
                    TableData tableData2 = new TableData(scope, null);
                    children.add(tableData2);
                }
                TableData tableData1 = children.stream().filter(e -> e.getCode().equals(scope)).findFirst().get();
                String annotationInfoSettingId = UUID.randomUUID().toString();
                TableData tableData2 = new TableData(classPath, "INSERT INTO annotation_info_setting (id, class_path, class_short_name, framework_id, scope, soa_type, effect, is_delete, other_params) VALUES ('" + annotationInfoSettingId + "', '" + classPath + "', '" + class_short_name + "', '" + key + "', '" + scope + "', '" + (StrUtil.isEmpty(soa_type) ? "" : soa_type)  + "', '" + effect + "', 0, '" + (StrUtil.isEmpty(other_params) ? "" : other_params) + "');");
                tableData1.getChildren().add(tableData2);
                Map<String, TableData> objectObjectHashMap2 = new HashMap<>();
                objectObjectHashMap2.put(annotationInfoSettingId, tableData2);
                objectObjectHashMap1.put(annotationInfoSettingIdOld, objectObjectHashMap2);
            });

        }

        ResultSet annotationParamSettingResultSet = state.executeQuery("select * from annotation_param_setting ;");

        while (annotationParamSettingResultSet.next()) {
            String annotationParamSettingName = annotationParamSettingResultSet.getString("name");
            String type = annotationParamSettingResultSet.getString("type");
            String describes = annotationParamSettingResultSet.getString("describe");
            String default_value = annotationParamSettingResultSet.getString("default_value");
            String parsing_type = annotationParamSettingResultSet.getString("parsing_type");
            String annotation_info_setting_id = annotationParamSettingResultSet.getString("annotation_info_setting_id");

            Map<String, TableData> stringTableDataMap = objectObjectHashMap1.get(annotation_info_setting_id);

            try {
                stringTableDataMap.forEach((key, value) -> {
                    TableData tableData3 = new TableData(annotationParamSettingName, "INSERT INTO annotation_param_setting (name, type, describe, annotation_info_setting_id, default_value, parsing_type) VALUES ('" + annotationParamSettingName + "', '" + type + "',' " + (StrUtil.isEmpty(describes) ? "" : describes) + "', '" + key + "', '" + (StrUtil.isEmpty(default_value) ? "" : default_value) + "', '" + (StrUtil.isEmpty(parsing_type) ? "" : parsing_type) + "');");
                    value.getChildren().add(tableData3);
                });
            } catch (Exception e) {
                System.out.printf("");
            }
        }
        objectObjectHashMap.values().stream().forEach(e -> {

            e.forEach((key, value) -> {
                initTableInfo.getAnnotations().add(value);
            });

        });

        System.out.printf("");

    }

}
